﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
		class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="125px" label-position="right" :rules="rules">
				<el-col :span="24">
					<el-form-item label="应用编码" prop="enCode">
						<el-input v-model='dataForm.enCode' placeholder='请输入' clearable required :style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="应用名称" prop="fullName">
						<el-input v-model='dataForm.fullName' placeholder='请输入' clearable required
							:style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="账号/app_key" prop="key">
						<el-input v-model='dataForm.key' placeholder='请输入' clearable required :style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="密码/app_secret" prop="secret">
						<el-input v-model='dataForm.secret' placeholder='请输入' clearable required :style='{ "width": "100%" }'>
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="获取Token接口" prop="interfaceId">
						<el-select v-model='dataForm.interfaceId' placeholder='请选择' clearable :style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in interfaceIdOptions" :key="index" :label="item.fullName"
								:value="item.id"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="证书" prop="certificate">
						<SHZY-UploadFz v-model='dataForm.certificate' showTip :fileSize='10' sizeUnit='MB' :limit='5'
							buttonText='点击上传'>
						</SHZY-UploadFz>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="有效标志" prop="enabledMark">
						<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0'>
						</el-switch>
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { getAuthenticationInfo, createAuthentication, updateAuthentication } from '@/api/system/interface/authentication'
export default {
	components: {},
	props: [],
	data() {
		return {
			btnLoading: false,
			loading: false,
			visible: false,
			dataForm: {
				id: '',
				enCode: undefined,
				fullName: undefined,
				key: undefined,
				secret: undefined,
				interfaceId: undefined,
				certificate: [],
				enabledMark: 1,
			},
			rules: {
				enCode: [
					{
						required: true,
						message: '请输入应用编码',
						trigger: '"blur"'
					},
				],
				fullName: [
					{
						required: true,
						message: '请输入应用名称',
						trigger: '"blur"'
					},
				],
				key: [
					{
						required: true,
						message: '请输入账号/app_key',
						trigger: '"blur"'
					},
				],
				secret: [
					{
						required: true,
						message: '请输入密码/app_secret',
						trigger: '"blur"'
					},
				],
			},
			interfaceIdOptions: [{ 'fullName': "选项一", 'id': "1" }, { 'fullName': "选项二", 'id': "2" }],
		}
	},
	computed: {},
	watch: {},
	created() {
	},
	mounted() {
	},
	methods: {
		goBack() {
			this.$emit('refresh')
		},
		init(id) {
			this.dataForm.id = id || 0;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['elForm'].resetFields();
				if (this.dataForm.id) {
					getAuthenticationInfo(this.dataForm.id).then(res => {
						this.dataForm = res.data;
						if (!this.dataForm.certificate) this.dataForm.certificate = [];
					})
				}
			});
		},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;
					const formMethod = !this.dataForm.id ? createAuthentication : updateAuthentication;

					formMethod(this.dataForm).then((res) => {
						this.btnLoading = false;
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})
				}
			})
		},
	}
}
</script>
